import { Component, OnInit } from '@angular/core';
import { NzModalService, NzModalRef } from 'ng-zorro-antd/modal';
import { ModalProgressContentComponent } from './modal-progress-content/modal-progress-content.component';
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
    selector: 'app-modal-progress',
    templateUrl: './modal-progress.component.html',
    styleUrls: ['./modal-progress.component.less'],
})
export class ModalProgressComponent implements OnInit {
    timer;
    progressModal: NzModalRef | null = null;
    cur = 0;

    constructor(
        private modalService: NzModalService,
        private message: NzMessageService
    ) {}

    ngOnInit(): void {
        // this.pollingOperation();
    }

    pollingOperation() {
        this.timer = setInterval(() => {
            this.getData();
        }, 1000);
    }

    getData() {
        this.cur += 10;
        if (this.cur <= 100) {
            this.showProcess('delete', this.cur); // 传递数值类型
        } else {
            clearInterval(this.timer);
            this.message.create('success', `操作成功！`);
        }
    }

    showProcess(type: 'delete' | 'restore', value: number): void {
        const modalTitle = type === 'delete' ? '删除进度' : '恢复进度';
        const isComplete = value === 100;

        if (!this.progressModal) {
            this.progressModal = this.modalService.info({
                nzTitle: modalTitle,
                nzContent: ModalProgressContentComponent,
                nzComponentParams: {
                    percent: value,
                },
                nzOkDisabled: !isComplete,
                nzClosable: false,
                nzOnOk: () => {
                    console.log('Info OK');
                    this.progressModal = null; // 重置模态框引用
                    if (this.timer) {
                        clearInterval(this.timer);
                    }
                },
            });
        } else {
            console.log('进度在更新...', value);
            const instance = this.progressModal.getContentComponent();
            instance ? (instance.percent = value) : null;
            this.progressModal.updateConfig({
                nzOkDisabled: !isComplete,
            });
        }
    }
}
